package com.kang.manager.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class MybatisPlusConfig {

	@Value("${spring.datasource.db-type:MYSQL}")
	private String dbType;

	@Bean
	public MybatisPlusInterceptor plusInterceptor() {
		try {
			DbType databaseType = DbType.valueOf(dbType.toUpperCase());
			MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
			mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(databaseType));
			return mybatisPlusInterceptor;
		} catch (IllegalArgumentException e) {
			// 日志记录或抛出自定义异常
			throw new RuntimeException("Unsupported database type: " + dbType, e);
		}
	}
}
